package com.sam.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dejian.platform.R;
import com.sam.widget.button.StateBitmapImageButton;

public class HeaderTitleBar extends RelativeLayout {
    private final Context context;
    private OnHeaderClickListener onHeaderClickListener = null;
    private Boolean showLeftBtn;
    private Boolean showTitle;
    private Boolean showRight;
    private StateBitmapImageButton leftBtn;
    private StateBitmapImageButton rightBtn;
    private TextView titleView;
    private TextView rightTextView;

    public HeaderTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getAttrs(context, attrs);
        findView();
        initView();
    }

    private void getAttrs(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HeaderTitleBar);
        showLeftBtn = ta.getBoolean(R.styleable.HeaderTitleBar_showLeftBtn, true);
        showTitle = ta.getBoolean(R.styleable.HeaderTitleBar_showTitle, true);
        showRight = ta.getBoolean(R.styleable.HeaderTitleBar_showRightBtn, false);
        ta.recycle();
    }

    private void findView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.widget_header_title_bar, this);
        leftBtn = (StateBitmapImageButton) findViewById(R.id.header_left_btn);
        rightBtn = (StateBitmapImageButton) findViewById(R.id.header_right_btn);
        titleView = (TextView) findViewById(R.id.header_title);
        rightTextView = (TextView) findViewById(R.id.header_right_text);
    }

    private void initView() {
        leftBtn.setVisibility(showLeftBtn ? VISIBLE : GONE);
        titleView.setVisibility(showTitle ? VISIBLE : GONE);
        rightBtn.setVisibility(showRight ? VISIBLE : GONE);
        rightTextView.setVisibility(showRight ? VISIBLE : GONE);
        leftBtn.setOnClickListener(onClickListener);
        rightBtn.setOnClickListener(onClickListener);
        rightTextView.setOnClickListener(onClickListener);
    }

    private final OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.header_left_btn) {
                if (onHeaderClickListener != null) onHeaderClickListener.onHeaderLeftViewClick();
                else if (context instanceof Activity) ((Activity) context).finish();
            } else if (v.getId() == R.id.header_title) {
                if (onHeaderClickListener != null) onHeaderClickListener.onHeaderCenterViewClick();
            } else if (v.getId() == R.id.header_right_text) {
                if (onHeaderClickListener != null) onHeaderClickListener.onHeaderRightViewClick();
            } else if(v.getId() ==  R.id.header_right_btn) {
                if (onHeaderClickListener != null) onHeaderClickListener.onHeaderRightViewClick();
            }
        }
    };

    public void setOnHeaderClickListener(OnHeaderClickListener onHeaderClickListener) {
        this.onHeaderClickListener = onHeaderClickListener;
    }

    public void setLeftVisibility(int visibility) {
        if (leftBtn.getVisibility() != visibility)
            leftBtn.setVisibility(visibility);
    }

    public void setCenterVisibility(int visibility) {
        if (titleView.getVisibility() != visibility)
            titleView.setVisibility(visibility);
    }

    public void setRightVisibility(int visibility) {
        if (rightBtn.getVisibility() != visibility)
            rightBtn.setVisibility(visibility);
        if(rightTextView.getVisibility() != visibility)
            rightTextView.setVisibility(visibility);
    }

    public void setTitle(String title){
        titleView.setText(title);
    }

    public void setTitle(int titleResId){
        titleView.setText(titleResId);
    }

    public void setRightImg(int srcImgId){
        rightBtn.setVisibility(VISIBLE);
        rightTextView.setVisibility(GONE);
        rightBtn.setImageResource(srcImgId);
    }

    public void setRightText(int textResId){
        rightBtn.setVisibility(GONE);
        rightTextView.setVisibility(VISIBLE);
        rightTextView.setText(textResId);
    }

    public void setRightText(String text){
        rightBtn.setVisibility(GONE);
        rightTextView.setVisibility(VISIBLE);
        rightTextView.setText(text);
    }

    public interface OnHeaderClickListener {
        void onHeaderLeftViewClick();

        void onHeaderCenterViewClick();

        void onHeaderRightViewClick();
    }
}
